package Homework;

// 2、无头双向链表实现
public class LinkedList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(last == null){
            last = node;
            head = node;
            return;
        }
        node.prev = last;
        last.next = node;
        last = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == size()){
            addLast(data);
            return true;
        }
        if(index > size() || index < 0){
            System.out.println("index不合法");
            return false;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        while(index > 0){
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }else{
                cur = cur.next;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.val == key){
            head = head.next;
            head.prev.next = null;
            head.prev = null;
            return;
        }
        ListNode cur = head.next;
        while(contains(key) && cur.next != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                cur.prev = cur.next = null;
                return;
            }
            else{
                cur = cur.next;
            }
        }
        if(last.val == key){
            last = last.prev;
            last.next.prev = null;
            last.next = null;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head.val == key){
            head = head.next;
            head.prev.next = null;
            head.prev = null;
        }
        ListNode cur = head.next;
        while(contains(key) && cur.next != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                cur.prev = cur.next = null;
            }
            else{
                cur = cur.next;
            }
        }
        if(last.val == key){
            last = last.prev;
            last.next.prev = null;
            last.next = null;
        }
    }
    //得到单链表的长度
    public int size(){
        if(head == null){
            return 0;
        }
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    public void display(){
        if(head == null){
            System.out.println("链表已清空:clear()");
            return;
        }
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        this.head = null;
        this.last = null;
    }
}

